// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import "Applications"

AndroidItem {
    id: _root

    property string wallpaperPath: ""           //壁紙
    property int currentScreen: 0           //現在の表示位置 0=センター
    property real default_x: 0

    property int numberOfHomescreen: 5
    property int moveLimit: 2
    property variant pos_list: [360, 180, 0, -180, -360]
    property real pos_left_limit: pos_list[0] * viewScale
    property real pos_right_limit: pos_list[moveLimit*2] * viewScale

    signal clicked(int key)
    signal startApplication(string packageName)

    Component.onCompleted: {
        default_x = x;
    }

    //ホームの枚数設定
    onNumberOfHomescreenChanged: {
        var num = numberOfHomescreen;
        if(isPortrait){
            switch(num){
            case 1:
                pos_list = [0];
                break;
            case 3:
                pos_list = [360, 0, -360];
                break;
            case 5:
                pos_list = [360, 180, 0, -180, -360];
                break;
            case 7:
                pos_list = [360, 240, 120, 0, -120, -240, -360];
                break;
            default:
                num = 5;
                break;
            }
        }else{
            pos_list = [0];
            num = 1;
        }

        moveLimit = (num - 1) / 2;
    }

    //ホームの画面を移動
    function move(pos){
        if(pos < -1 * moveLimit){
            pos = -1 * moveLimit;
        }else if(pos > moveLimit){
            pos = moveLimit;
        }
        _wallpaperAnim.to = pos_list[pos + moveLimit] * viewScale;
        _wallpaperAnim.start();

        currentScreen = pos;
    }

    //壁紙
    Item{
        x: _root.width / 2 - width / 2
        y: -1 * 50 * 2 * viewScale
        width: 1440 * viewScale
        height: 1280 * viewScale

        Image {
            id: _wallpaper
            x: 0//_root.width / 2 - width / 2
            y: 0
            width: parent.width
            height: parent.height
            source: wallpaperPath
            clip: true
            fillMode: Image.PreserveAspectCrop
            smooth: true
            cache: false

            onXChanged: {
                //アクティブバーの位置
                _key.activeBarPosition = 1.0 - (_wallpaper.x - pos_right_limit) / (pos_left_limit - pos_right_limit);
            }
        }
        //壁紙の左右移動
        NumberAnimation {
            id: _wallpaperAnim
            running: false
            target: _wallpaper
            property: "x"
            to: 0
            duration: 200
            easing.type: Easing.OutSine
        }
    }
    MouseArea{
        anchors.fill: parent
        property real start_x: 0
        property real prev_x: 0
        onPressed: {
            start_x = mouse.x;
            prev_x = mouse.x;
            _wallpaperAnim.stop();
        }
        onMousePositionChanged: {
            var temp = _wallpaper.x + (mouse.x - prev_x) * 180 / 360 * viewScale;
            if(temp < pos_right_limit){
                //右端
                temp = pos_right_limit;
                _rightEdge.opacity = (start_x - mouse.x) / (720 * viewScale);
            }else if(temp > pos_left_limit){
                //左端
                temp = pos_left_limit;
                _leftEdge.opacity = (mouse.x - start_x) / (720 * viewScale);
            }else{
                _leftEdge.opacity = 0;
            }
            //壁紙の位置
            _wallpaper.x = temp;
            prev_x = mouse.x;

            _key.showActiveBar();   //アクティブバーを表示する
        }
        onReleased: {
            var border = 180 * viewScale;
            if((start_x - mouse.x) > border){
                currentScreen += 1;
            }else if((start_x - mouse.x) < -1 * border){
                currentScreen -= 1;
            }else{
            }
            move(currentScreen);

            _leftEdge.reset();      //端の四角を消す
            _rightEdge.reset();     //端の四角を消す
            _key.hideActiveBar();   //バーの光ってるところを消す
        }
    }

    //キーとか
    HomeKey{
        id: _key
        anchors.fill: parent
        numberOfHomescreen: _root.numberOfHomescreen
        viewScale: _root.viewScale
        onClicked: {
            _root.clicked(key);
        }
        onStartApplication: {
            _root.startApplication(packageName);
        }
    }

    //検索ウィジェット
    ComGoogleAndroidGooglequicksearchboxWidget{
        anchors.horizontalCenter: parent.horizontalCenter
        viewScale: _root.viewScale
        isLock: _root.isLock
        onClicked: {
            _root.startApplication("com.google.android.googlequicksearchbox");
        }
    }


    //ホーム端の行き止まり画像
    HomeMoveEdge{
        id: _leftEdge
        viewScale: _root.viewScale
        isLeft: true
    }
    HomeMoveEdge{
        id: _rightEdge
        viewScale: _root.viewScale
        isLeft: false
    }


    states: [
        State {
            name: "Land"
            when: !isPortrait
            PropertyChanges {
                target: _root
                numberOfHomescreen: 1
                moveLimit: 0
                pos_list: [0]
            }
        }
    ]
}
